﻿using System;
using System.IO;
using System.Text;

[LuaExport]
class ByteArray
{
    public static ByteArray Create()
    {
        return new ByteArray();
    }
    private MemoryStream innerStream;
    private BinaryReader reader;
    private BinaryWriter writer;
    public ByteArray()
    {
        innerStream = new MemoryStream();
        reader = new BinaryReader(innerStream);
        writer = new BinaryWriter(innerStream);
    }
    public ByteArray(byte[] bytes)
    {
        innerStream = new MemoryStream(bytes);
        reader = new BinaryReader(innerStream);
        writer = new BinaryWriter(innerStream);
    }

    public int Position
    {
        get
        {
            return (int)innerStream.Position;
        }
        set
        {
            innerStream.Position = value;
        }
    }
    public int Length
    {
        get
        {
            return (int)innerStream.Length;
        }
    }

    public int ReadByte()
    {
        return reader.ReadByte();
    }

    public bool ReadBool()
    {
        return reader.ReadByte() != 0;
    }

    public int ReadShort()
    {
        return System.Net.IPAddress.NetworkToHostOrder(reader.ReadInt16());
    }
    public int ReadInt()
    {
        return System.Net.IPAddress.NetworkToHostOrder(reader.ReadInt32());
    }
    public long ReadLong()
    {
        return System.Net.IPAddress.NetworkToHostOrder(reader.ReadInt64());
    }
    public float ReadFloat()
    {
        int rint = System.Net.IPAddress.NetworkToHostOrder(reader.ReadInt32());
        byte[] bytes = BitConverter.GetBytes(rint);
        return BitConverter.ToSingle(bytes, 0);
    }
    public double ReadDouble()
    {
        long rint = System.Net.IPAddress.NetworkToHostOrder(reader.ReadInt64());
        byte[] bytes = BitConverter.GetBytes(rint);
        return BitConverter.ToDouble(bytes, 0);
    }
    public string ReadString()
    {
        int len = ReadShort();
        byte[] bytes = reader.ReadBytes(len);
        return Encoding.UTF8.GetString(bytes);
    }
    public void WriteByte(byte value)
    {
        writer.Write(value);
    }
    public void WriteBool(bool value)
    {
        writer.Write(value);
    }
    public void WriteShort(short value)
    {
        writer.Write(System.Net.IPAddress.HostToNetworkOrder(value));
    }
    public void WriteInt(int value)
    {
        writer.Write(System.Net.IPAddress.HostToNetworkOrder(value));
    }
    public void WriteLong(long value)
    {
        writer.Write(System.Net.IPAddress.HostToNetworkOrder(value));
    }
    public void WriteFloat(float value)
    {
        byte[] bytes = BitConverter.GetBytes(value);
        int rint = System.Net.IPAddress.HostToNetworkOrder(BitConverter.ToInt32(bytes, 0));
        writer.Write(rint);
    }
    public void WriteDouble(double value)
    {
        byte[] bytes = BitConverter.GetBytes(value);
        long rint = System.Net.IPAddress.HostToNetworkOrder(BitConverter.ToInt64(bytes, 0));
        writer.Write(rint);
    }
    public void WriteString(string value)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(value);
        WriteShort((short)bytes.Length);
        writer.Write(bytes);
    }
    public byte[] ToArray()
    {
        return innerStream.ToArray();
    }
}

